/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.migrate;

import com.wtkj.vo.dzzf.base.kfwd.FundAccount;
import com.wttech.dzzf.process.dao.ImportHisDataDao;
import com.wttech.dzzf.process.model.AccountKey;
import com.wttech.dzzf.process.model.user.AccountNameKey;
import com.wttech.dzzf.process.service.base.BaseInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * 合并重复的数据
 *
 * @author MaShibo
 * @date 2017/12/10.
 */
@Service
public class ImportRepeatHisDataService {

	private static final Logger LOGGER = LoggerFactory.getLogger(ImportRepeatHisDataService.class);

	@Resource
	private ImportHisDataDao importHisDataDao;

	@Resource
	private ThreadPoolTaskExecutor executor;

	@Resource
	private BaseInfoService baseInfoService;

	@Resource
	private MergeFundAccountService mergeFundAccountService;

	public void process() {
		List<AccountKey> accountKeys = importHisDataDao.queryAccountKeys();
		int total = accountKeys.size();
		LOGGER.info("共{}个重复的账户信息待处理", total);
		List<Future<Void>> futures = new ArrayList<>(total);
		accountKeys.forEach(accountKey -> futures.add(executor.submit(() -> processSingleAccountKey(accountKey))));
		baseInfoService.getFutureResult(futures);
		LOGGER.info("重复账户信息处理完毕");
		executor.shutdown();
	}

	private void processsNullCert() {
		List<AccountNameKey> nameKeys = importHisDataDao.queryAccountNameKeys();
		int total = nameKeys.size();
		LOGGER.info("共{}个重复的无证件号账户信息待处理", total);
		List<Future<Void>> futures = new ArrayList<>(total);
		nameKeys.forEach(accountNameKey -> futures.add(executor.submit(() -> processSingleAccountNameKey(accountNameKey))));
		baseInfoService.getFutureResult(futures);
		LOGGER.info("重复无证件号账户信息处理完毕");
		executor.shutdown();
	}

	private Void processSingleAccountNameKey(AccountNameKey ank) {
		String fundAccountName = ank.getFundAccountName();
		String mobile = ank.getMobile();
		LOGGER.info("正在处理账户名【{}】手机号【{}】的信息", fundAccountName, mobile);
		try {
			List<FundAccount> fundAccounts = importHisDataDao.queryFundAccountsByName(fundAccountName, mobile);
			mergeFundAccountService.process(fundAccounts);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			LOGGER.error("账户名信息【{}】处理异常", ank);
		}
		return null;
	}

	private Void processSingleAccountKey(AccountKey ak) {
		String certifiNo = ak.getCertifiNo();
		Integer certifiType = ak.getCertifiType();
		LOGGER.info("正在处理重复证据号【{}】，证件类型【{}】", certifiNo, certifiType);
		try {
			List<FundAccount> fundAccountList = importHisDataDao.queryFundAccountsByCert(certifiNo, certifiType);
			mergeFundAccountService.process(fundAccountList);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
			LOGGER.error("证件信息【{}】处理异常", ak);
		}
		return null;
	}

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		ImportRepeatHisDataService service = context.getBean(ImportRepeatHisDataService.class);
		service.process();
	}
}
